package org.fing.edu.uy.esbadp.motor.strategies;

import java.util.List;
import java.util.logging.Level;

import org.fing.edu.uy.esbadp.core.AdpFlowInterface;
import org.fing.edu.uy.esbadp.core.AdpFlowRET;
import org.fing.edu.uy.esbadp.core.constants.EsbAdpServiceMetadataKeys;
import org.fing.edu.uy.esbadp.core.entity.DMRegisteredService;
import org.fing.edu.uy.esbadp.core.entity.GenericTreeNode;
import org.fing.edu.uy.esbadp.core.entity.PropertiesMonitoring;
import org.fing.edu.uy.esbadp.core.interfaces.IAdpStrategy;
import org.fing.edu.uy.esbadp.logger.LoggerADP;
import org.fing.edu.uy.esbadp.motor.comunication.AdmAdaptationComunication;

public class AdpStrategyDelayService implements IAdpStrategy {

	private static final java.util.logging.Logger logger = LoggerADP.getLogger(AdpStrategyDelayService.class.getSimpleName());
	
	/*
	 * Tree Nodes = RET + VirtualService
	 */
	@Override
	public GenericTreeNode<AdpFlowInterface> getAdpTree(DMRegisteredService service,List<DMRegisteredService> equivServices, PropertiesMonitoring properties) {
		try{
			GenericTreeNode<AdpFlowInterface> root = new GenericTreeNode<AdpFlowInterface>();
			AdpFlowInterface action = new AdpFlowRET(calcDelayTime(service));
			root.setData(action);	
			GenericTreeNode<AdpFlowInterface> virtualServiceNode = AdmAdaptationComunication.getAdpNodeForInvokeVirtualService(service);
			if (virtualServiceNode != null){
				root.addChild(virtualServiceNode);		
				return root;
			}else{
				logger.log(Level.SEVERE, "Error in getAdpNodeForInvokeVirtualService");
				return null;
			}
		}catch(Exception ex){
			logger.log(Level.SEVERE, "Exception in AdpStrategyDelayService...\n StackTarce => [ {0} ]", ex);	
			return null;
		}
	}

	private long calcDelayTime(DMRegisteredService service) {		
		String delay = service.getMetadata(EsbAdpServiceMetadataKeys.dataDelayTime,EsbAdpServiceMetadataKeys.dataDelayTimeDefaultValue);
		return Long.parseLong(delay);
	} 
	
}
